<?php

namespace util;

class Crypt {
    public static $cipher = MCRYPT_RIJNDAEL_128;
    public static $key = 'my.oss.com/portal/CRYPT/RIJNDAEL';
    public static $mode = MCRYPT_MODE_CBC;
    public static $iv = 'my.oss.com/oss22';

    public static function set_config($config) {
        foreach ($config as $k => $v) {
            self::$k = $v;
        }
    }

    public static function encrypt($str) {
        $encrypt = '';
        $encrypt = mcrypt_encrypt(self::$cipher, self::$key, $str, self::$mode, self::$iv);
        $encrypt = bin2hex($encrypt);

        return $encrypt;
    }

    public static function decrypt($str) {
        $decrypt = '';
        for($i = 0; $i < strlen($str); $i += 2) {
            $decrypt .= chr(hexdec(substr($str, $i, 2)));
        }
        $decrypt = mcrypt_decrypt(self::$cipher, self::$key, $decrypt, self::$mode, self::$iv);

        $str = '';
        $decrypt = str_split($decrypt);
        for($i = 0; $i < count($decrypt); $i++) {
            ord($decrypt[$i]) === 0 or $str .= $decrypt[$i];
        }

        return $str;
    }

    public static function md5_16($str) {
        return substr(md5($str), 8, 16);
    }
}
